/**
 * 
 */
package com.yst.metho.web.application.projectversion.views;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.NamingException;

import com.yst.metho.web.application.interfaces.IListingViewModel;
import com.yst.metho.web.application.projectversion.actions.folder.CreateFolderAction;
import com.yst.metho.web.application.projectversion.actions.folder.EditPrjFolderAction;
import com.yst.metho.web.application.utils.AbstractSuiviComparator;
import com.yst.metho.web.application.utils.AbstractSuiviTableModel;
import com.yst.metho.web.application.utils.Locator;
import com.yst.suivi.data.folders.Folder;
import com.yst.suivi.data.project.Project;

/**
 * @author perso
 * 
 */
public class ProjectClassificationListModel extends AbstractSuiviTableModel implements IListingViewModel
{
	private class ProjectClassificationComparator extends AbstractSuiviComparator
	{

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
		 */
		public int compare(Object o1, Object o2)
		{
			Folder u1 = (Folder) o1;
			Folder u2 = (Folder) o2;
			int comparaison = 0;
			switch (getColumn())
			{
			case 0:
				comparaison = u1.getNom().compareToIgnoreCase(u2.getNom());
				break;
			default:
				break;
			}
			if (!isAscending())
			{
				comparaison = -1 * comparaison;
			}
			return comparaison;
		}

	}

	private String[] mColumnsName =
	{ "Nom" };

	// private Collection mClassificationsList;

	private Project mPrj;

	public ProjectClassificationListModel(Project prj) throws NamingException, FinderException, CreateException
	{
		super();
		mPrj = prj;
		setComparator(new ProjectClassificationComparator());
		refresh();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.interfaces.IListingViewModel#getToolbarAction()
	 */
	public List getToolbarAction()
	{
		ArrayList actions = new ArrayList();
		actions.add(new CreateFolderAction(this, mPrj));
		actions.add(new EditPrjFolderAction(this, mPrj));
		return actions;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.interfaces.IListingViewModel#refresh()
	 */
	public void refresh()
	{
		try
		{
			mList = Locator.getFolderView().findFolder(mPrj.getId());
			fireTableDataChanged();
		}
		catch (NamingException e)
		{
			throw new RuntimeException(e);
		}
		catch (FinderException e)
		{
			throw new RuntimeException(e);
		}
		catch (CreateException e)
		{
			throw new RuntimeException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nextapp.echo2.app.table.AbstractTableModel#getColumnName(int)
	 */
	public String getColumnName(int arg0)
	{
		return mColumnsName[arg0];
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nextapp.echo2.app.table.TableModel#getColumnCount()
	 */
	public int getColumnCount()
	{
		return mColumnsName.length;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nextapp.echo2.app.table.TableModel#getRowCount()
	 */
	// public int getRowCount()
	// {
	// int retour = 0;
	// if (null != mClassificationsList)
	// {
	// retour = mClassificationsList.size();
	// }
	// return retour;
	// }
	/*
	 * (non-Javadoc)
	 * 
	 * @see nextapp.echo2.app.table.TableModel#getValueAt(int, int)
	 */
	// public Object getValueAt(int column, int row)
	// {
	// Object retour = null;
	// Iterator it = mClassificationsList.iterator();
	// int i = -1;
	// do
	// {
	// retour = it.next();
	// i++;
	// }
	// while (i != row);
	// return retour;
	// }
}
